const { $util } = getApp();
const MAX_LENGTH = 10;
let isPrevImg = false;
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    typ: 0,
    page: 1,
    list: [],
    isPrev: true,
    isSearch: false,
    hasMore: true,
    nav: [{ id: 0, title: '推荐' }, { id: 1, title: '最新' }, { id: 2, title: '我的' }],
    listIdx: -1, // 查看的帖子的 index
    useTabbar: !(app.tabbarindex === 0 && app.siteInfo.tabbar_type === '1'),
  },
  onLoad(ops) {
    ops.barTitle && wx.setNavigationBarTitle({
      title: decodeURIComponent(ops.barTitle),
    });
  },
  onShow() {
    let wxData = wx.getStorageSync('qyUserInfo') || {};
    this.setData(wxData);
    this.setData({
      isSaaS: wx.getStorageSync('isSaaS'),
    })
    if (isPrevImg) {
      isPrevImg = false;
    }
    if (this.data.listIdx === -1) {
      this.setData({
        page: 1,
      }, () => {
        this.loadTopic();
      });
    } else {
      this.loadDetail();
    }
  },
  changeNav(e) {
    this.setData({
      page: 1,
      typ: e.detail.obj.id,
    }, () => {
      this.getList();
    });
  },
  loadTopic() {
    $util.request({
      url: 'entry/wxapp/group.topic.gettoppiclist',
      data: {},
      showLoading: false,
      success: (res) => {
        this.getList();
        this.setData({
          topList: res.data,
        });
      },
    });
  },
  getList() {
    let { typ: type, page } = this.data;
    $util.request({
      url: type === 2 ? 'entry/wxapp/group.comment.mycommentlist' : 'entry/wxapp/group.comment.getnewcommentlist',
      data: {
        type,
        page,
      },
      showLoading: false,
      success: (res) => {
        let list = res.data;
        if (this.data.page > 1) {
          list = [...this.data.list, ...list];
        }
        this.setData({
          list,
          hasMore: !((this.data.page !== 1 && res.data.length !== MAX_LENGTH)),
          loaded: true,
          page: res.data.length ? this.data.page + 1 : this.data.page,
        });
      },
    });
  },
  bindlike(e) {
    const { id, idx } = e.currentTarget.dataset;
    $util.request({
      url: 'entry/wxapp/group.comment.setlike',
      data: {
        commentid: id,
      },
      showLoading: false,
      success: (res) => {
        const { list } = this.data;
        list[idx].islike = Number(res.code);
        list[idx].likenum = Number(res.allnum);
        this.setData({
          list,
        });
      },
    });
  },
  bindDelete(e) {
    const id = e.currentTarget.dataset.id;
    const idx = e.currentTarget.dataset.idx;
    wx.showModal({
      title: '提示',
      content: '确定要删除这条帖子吗？',
      success: (result) => {
        if (result.confirm) {
          $util.request({
            url: 'entry/wxapp/group.comment.mycommentdelete',
            data: {
              commentid: id,
            },
            showLoading: false,
            success: (res) => {
              const list = this.data.list;
              list.splice(idx, 1);
              this.setData({
                list,
              });
              wx.showToast({
                title: '删除成功',
                icon: 'none',
              });
            },
          });
        }
      },
    });
  },
  onPrevImg() {
    isPrevImg = true;
  },
  goDetail(e) {
    let { id, idx } = e.currentTarget.dataset;
    this.setData({
      listIdx: idx,
      listId: id,
    }, () => {
      wx.navigateTo({
        url: `/ws_party_const/pages/topic/detail?id=${id}&idx=${idx}`,
      });
    });
  },
  loadDetail(e) {
    let { listId, listIdx } = this.data;
    $util.request({
      url: 'entry/wxapp/group.comment.commentshow',
      data: {
        cid: listId,
        page: 1,
      },
      showLoading: false,
      success: (res) => {
        this.setData({
          [`list[${listIdx}].viewnum`]: res.data.viewnum,
          [`list[${listIdx}].commentnum`]: res.data.commentnum,
          [`list[${listIdx}].likenum`]: res.data.likenum,
          listIdx: -1,
        });
      },
    });
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    this.data.hasMore && this.getList();
  },
  onShareAppMessage() {
    return {
      path: '/ws_party_const/pages/topic/index',
    };
  },
});
